<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class User extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('User');

    }
    /**
     * 用户管理 
     */
    public function index()
    {
        $map = array();
        $this->setMeta('系统管理|用户管理');
        return view();
    }
    /**
     * 用户管理列表页面
     * @return [type] [description]
     */
    public function lists(){
        $id = input('id/d');


        $map = array();
        $map['a.status'] = ['<>',-1];

        if( $id ==  0 ){
             $map['c.id'] = NULL;
        }else{
            $ids = model('Role')->tree->getids( $id );
            $map['c.id'] = ['in', $ids] ;
            
            // $map['c.dept_id'] = $dept_id ;
        }
        // $list = $this->model->where($map)->paginate(20,false,['query' => request()->param()]);

        $list = $this->model->alias('a')
            ->distinct(true)
            ->field('a.*')
            ->join('__USER_ROLE__ b','b.user_id = a.user_id','left')
            ->join('__ROLE__ c','c.id = b.role_id','left')
            ->where($map)
            // ->where( $where_dept )
            ->select();
    // echo $this->model->getLastSql();

        $this->assign('id',$id);
        $this->assign('list',$list);
        $this->setMeta('系统管理|用户管理');
        return view();
    }

    /**
     * 添加 or  修改
     * @return [type]           [description]
     */
    public function edit(){
        if(IS_AJAX){
            $data   = $this->request->post();

            $allowField = [ 'username' , 'status' ,'sex' ,'birthday' ,  'mobile' ,   'workphone'  ,  'qq'  ,   'email'  , 'idcard' , 'address' ];
            if(  !$data['user_id'] || isset($data['upt_password_checked'])){
                $allowField[] = 'password';
                if( empty($data['password']) ){ $this->error( '密码不能为空！！！' ); }
            }
            //判断是 添加 还是 修改
            if (!$data['user_id']) {
                $allowField[] = 'loginname';
                $scene = 'user.add';
            }else{
                $scene = 'user.edit';
            }
// dump($data);
            if ($data) {

                $result = $this->model
                    ->isUpdate(!!$data['user_id'])
                    ->validate($scene)
                    ->allowField($allowField )
                    ->save($data);

                if (false === $result) {
                    
                    return $this->error($this->model->getError() );
                } else {
                    // echo $this->model->getLastSql();
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( $this->model->getError() );
            }  
        }else{

            $id = input('id/d' , 0 );
            $dept_id = input('dept_id/d' , -1 );
            // $dopost = input('dopost/s' , '' );
            $info = $this->model-> get($id); //读取数据 



            $this->assign('info' , $info );
            $this->setMeta('系统管理|岗位管理');
            return view();
        }


        
    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {
        $id = array_unique((array) input('id/a', 0));

        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('user_id' => array('in', $id));

        // $del_user_role = db('user_role')->where( $map2 )->delete(); //用户表是假删除  不删除关系表

        if ( false === $this->model->where($map)->setField('status',-1) ) {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }

    /**
     * [role description]
     * @return [type] [description]
     */
    public function role(){
        $id = input('id/d', 0);
        $user = $this->model->get($id);



        $this->assign('info' , $user );
        $this->assign('list' , $user->role_arr );
        return view();
    }


    
    /**
     * [getRoles description]
     * @param  [type] $dept_id [description]
     * @return [type]          [description]
     */
    public function getRoles($dept_id){

        $dept_id = input('dept_id/d');
        $map['dept_id'] = $dept_id;
        $rs = db('role')->where($map)->select();
        $html ='';
        foreach($rs as $k => $v){
            $html .= "<option value=\"${v['id']}\">{$v['name']}</option>";
        }
       if( empty($html) ){
            $html = '<option value=\"0\">--没有岗位--</option>';
        }
        echo  $html;

    }
    /**
     * 
     * @return [type] [description]
     */
    public function addUserRole(){

        $data = array();
        $data['user_id'] = input('user_id/d');
        $data['role_id'] = input('role_id/d');
        $m = model('UserRole');


        if ($data) {

            $result = $m
                ->isUpdate(false)   //只添加不修改
                ->validate(true)
                ->allowField(true)
                ->save($data);

            if (false === $result) {
                return $this->error( $m->getError() );
            } else {
                // echo $this->model->getLastSql();
                return $this->success('添加成功');
            }
        } else {
            return $this->error( '数据不存在' );
        }  
    }

    public function delUserRole(){
        $data = array();
        $data['user_id'] = input('user_id/d');
        $data['role_id'] = input('role_id/d');
        $m = model('UserRole');  
        if ($data) {

            $result = $m->del( $data['user_id'] , $data['role_id'] );

            if (false === $result) {
                return $this->error( $m->getError() );
            } else {
                // echo $this->model->getLastSql();
                return $this->success('删除成功');
            }
        } else {
            return $this->error( '数据不存在' );
        }  

    }



    public function rule(){

        return '待开发。。。';

    }







}//end class
